home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Unix / macutil2.0b1.shar / macutil / util / transname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-21  |  4.4 KB  |  110 lines

  1. #include <sys/types.h>
  2. #include <sys/dir.h>
  3.  
  4. char *strncpy();
  5.  
  6. #ifdef MAXNAMLEN    /* 4.2 BSD */
  7. #define FNAMELEN MAXNAMLEN
  8. #else
  9. #define FNAMELEN DIRSIZ
  10. #endif
  11.  
  12. /* Create a Unix file name based on the Mac filename.  First off we have
  13.  * possible problems with filename sizes (Sys V) and also with allowable
  14.  * characters.  Mac filename characters can be anything from 1 to 254 (I
  15.  * think 0 and 255 are not allowed) with colon disallowed.  Unix filenames
  16.  * have also a lot of freedom, but cannot contain 0 or '/'.  Also on Unix
  17.  * non-printable are a trouble (you will never see the filename correctly;
  18.  * and it may even lock your terminal with some versions of Unix).
  19.  * So first off non-printable characters are mapped to underscore.
  20.  * Although there are Unix systems that allow the high order bit set in
  21.  * a filename character in all programs, nearly all implementations do not
  22.  * allow that, so also characters in the range 0200-0377 are mapped to
  23.  * underscore (except as noted below).  Some people would also like to
  24.  * remap characters that are special to some shells (open brackets,
  25.  * asterisks, exclamation point (csh), etc.)  I did elect not to do so
  26.  * because there is no end.  (The previous code disallowed a lot, but not
  27.  * the braces that are special to some shells, obviously he was a C-shell user!)
  28.  * Characters in the range 0200-0377 are in part accented letters
  29.  * (the Swedes, Norwegians and Danes would not agree, but who listens to
  30.  * them!); those are mapped to the unaccented version.  All other characters
  31.  * in this range are mapped to underscore.  Note: this is based on the
  32.  * Geneva font!
  33.  * This stuff is now largely table driven.
  34.  * One day I may modify this so that an environment variable may be used
  35.  * to define mappings. */
  36.  
  37. static char char_mapping[] = {
  38.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  39.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  40.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  41.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  42.      '_',  '!',  '"',  '#',  '$',  '%',  '&', '\'',
  43.      '(',  ')',  '*',  '+',  ',',  '-',  '.',  '_',
  44.      '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
  45.      '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
  46.      '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
  47.      'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
  48.      'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
  49.      'X',  'Y',  'Z',  '[', '\\',  ']',  '^',  '_',
  50.      '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
  51.      'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
  52.      'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
  53.      'x',  'y',  'z',  '{',  '|',  '}',  '~',  '_',
  54. #ifndef LATIN1
  55.      'A',  'A',  'C',  'E',  'N',  'O',  'U',  'a',
  56.      'a',  'a',  'a',  'a',  'a',  'c',  'e',  'e',
  57.      'e',  'e',  'i',  'i',  'i',  'i',  'n',  'o',
  58.      'o',  'o',  'o',  'o',  'u',  'u',  'u',  'u',
  59.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  60.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  'O',
  61.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  62.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  'o',
  63.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  64.      '_',  '_',  '_',  'A',  'A',  'O',  '_',  '_',
  65.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  66.      'y',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  67. #else /* LATIN1 */
  68.     0304, 0305, 0307, 0311, 0321, 0326, 0334, 0341,
  69.     0340, 0342, 0344, 0343, 0345, 0347, 0351, 0350,
  70.     0352, 0353, 0355, 0354, 0356, 0357, 0361, 0363,
  71.     0362, 0364, 0366, 0365, 0372, 0371, 0373, 0374,
  72.      '_', 0260, 0242, 0243, 0247, 0267, 0266, 0337,
  73.     0256, 0251,  '_', 0264, 0250,  '_', 0306, 0330,
  74.      '_', 0261,  '_',  '_', 0245,  '_',  '_',  '_',
  75.      '_',  '_',  '_',  '_',  '_',  '_', 0346, 0370,
  76.     0277, 0241, 0254,  '_',  '_',  '_',  '_', 0253,
  77.     0273,  '_',  '_', 0300, 0303, 0325,  '_',  '_',
  78.      '_',  '_',  '_',  '_',  '_',  '_', 0367, 0244,
  79.     0377,  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  80. #endif /* LATIN1 */
  81.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  82.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  83.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_',
  84.      '_',  '_',  '_',  '_',  '_',  '_',  '_',  '_'};
  85.  
  86. void transname(name, namebuf, n)
  87. char *name, *namebuf;
  88. int n;
  89. {
  90.     char *np;
  91.  
  92.     /* make sure host file name doesn't get truncated beyond recognition */
  93.     if (n > FNAMELEN - 2) {
  94.     n = FNAMELEN - 2;
  95.     }
  96.     (void)strncpy(namebuf, name, n);
  97.     namebuf[n] = '\0';
  98.  
  99.     /* now: translate name */
  100.     for (np = namebuf; *np; np++){
  101.     *np = char_mapping[*np & 0xff];
  102.     }
  103. #ifdef NODOT
  104.     if(*namebuf == '.') {
  105.     *namebuf = '_';
  106.     }
  107. #endif /* NODOT */
  108. }
  109.  
  110.